﻿using FieldTools.MediaFindOut.Control;
using System;
using System.Windows;
using System.Windows.Input;

namespace FieldTools.MediaFindOut.Style
{
    public partial class CustomWindow : ResourceDictionary
    {
        /// <summary>
        /// 拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomWindow_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                win.DragMove();
            }
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomWindowBtnClose_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            win.Close();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomWindowBtnMinimized_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            win.WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 最大化、还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomWindowBtnMaxNormal_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            if (win.WindowState == WindowState.Maximized)
            {
                win.WindowState = WindowState.Normal;
            }
            else
            {
                // 不覆盖任务栏
                win.MaxWidth = SystemParameters.WorkArea.Width;
                win.MaxHeight = SystemParameters.WorkArea.Height;
                win.WindowState = WindowState.Maximized;
            }
        }
        /// <summary>
        /// 置顶切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Top_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            win.Topmost = !win.Topmost;
            if (win.Topmost) MessageManage.Success("当前已置顶");
            else MessageManage.Success("已取消置顶");
        }
        /// <summary>
        /// 自定义主题
        /// </summary>
        ResourceDictionary? customTheme = null;
        /// <summary>
        /// 主题切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_ChangeTheme_Click(object sender, RoutedEventArgs e)
        {
            if (customTheme != null && customTheme.Count > 0)
            {
                var tag = customTheme["CustomTheme.Tag"];
                foreach (var item in customTheme.Keys)
                {
                    var str = item.ToString();
                    //切换为深色模式
                    if (Convert.ToInt32(customTheme["CustomTheme.Tag"]) == 1 && !string.IsNullOrEmpty(str) && str.StartsWith("CustomTheme.Theme.Dark.Color"))
                    {
                        customTheme[str.Replace(".Dark", string.Empty)] = customTheme[str];
                        tag = 2;
                    }
                    //切换为浅色模式
                    else if (Convert.ToInt32(customTheme["CustomTheme.Tag"]) == 2 && !string.IsNullOrEmpty(str) && str.StartsWith("CustomTheme.Theme.Ligth.Color"))
                    {
                        customTheme[str.Replace(".Ligth", string.Empty)] = customTheme[str];
                        tag = 1;
                    }
                }
                customTheme["CustomTheme.Tag"] = tag;
                if (tag.Equals(1)) MessageManage.Success("已设置为浅色模式");
                if (tag.Equals(2)) MessageManage.Success("已设置为深色模式");
            }
        }
        /// <summary>
        /// 界面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var dics = Application.Current.Resources.MergedDictionaries;
            for (int i = 0; i < dics.Count; i++)
            {
                var tempDic = dics[i];
                foreach (var item in tempDic.Keys)
                {
                    if (item.Equals("CustomTheme.Tag"))
                    {
                        customTheme = tempDic;
                    }
                    if (item.Equals("HeaderHeigth"))
                    {
                        HeaderHeigth = (GridLength)tempDic[item];
                    }
                }
            }
        }
        /// <summary>
        /// 标题栏高度
        /// </summary>
        public static GridLength HeaderHeigth { get; set; }
    }
}
